package com.jiayi.web.controller.cms;

import com.jiayi.api.JiayiNewsService;
import com.jiayi.common.annotation.Log;
import com.jiayi.common.core.domain.AjaxResult;
import com.jiayi.common.core.page.TableDataInfo;
import com.jiayi.common.developutil.ConvertUtils;
import com.jiayi.common.enums.BusinessType;
import com.jiayi.data.converter.JiayiNewsConverter;
import com.jiayi.data.entity.request.JiayiNewsRequest;
import com.jiayi.data.entity.request.JiayiNewsSearchPageRequest;
import com.jiayi.data.entity.request.JiayiNewsSearchRequest;
import com.jiayi.data.entity.schema.JiayiNews;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 新闻动态Controller
 *
 * @author zzx
 * @date 2024-11-26
 */
@RestController
@RequestMapping("/cms/jiayi_news")
public class JiayiNewsController {
    @Autowired
    private JiayiNewsService jiayiNewsService;

    @Autowired
    private JiayiNewsConverter jiayiNewsconverter;

    /**
     * 查询新闻动态列表 分页
     */
    @PreAuthorize("@ss.hasPermi('cms:jiayi_news:list')")
    @PostMapping("/listPage")
    public TableDataInfo listPage(@RequestBody JiayiNewsSearchPageRequest request) {
        return jiayiNewsService.selectJiayiNewsList(request);
    }


    /**
     * 查询新闻动态列表 不分页
     */
    @PreAuthorize("@ss.hasPermi('cms:jiayi_news:list')")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody JiayiNewsSearchRequest request) {
        List<JiayiNews> list = jiayiNewsService.selectJiayiNewsList(request);
        return AjaxResult.success(ConvertUtils.convert(list, jiayiNewsconverter::convertToJiayiNewsDTO));
    }

    /**
     * 导出新闻动态列表
     */
    @PreAuthorize("@ss.hasPermi('cms:jiayi_news:export')")
    @Log(title = "新闻动态" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@RequestBody JiayiNewsSearchRequest request, HttpServletResponse response) {
        jiayiNewsService.export(request, response);
    }

    /**
     * 获取新闻动态详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:jiayi_news:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(jiayiNewsService.selectJiayiNewsById(id));
    }

    /**
     * 新增新闻动态
     */
    @PreAuthorize("@ss.hasPermi('cms:jiayi_news:add')")
    @Log(title = "新闻动态" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JiayiNewsRequest request) {
        return AjaxResult.toAjax(jiayiNewsService.insertJiayiNews(request));
    }

    /**
     * 修改新闻动态
     */
    @PreAuthorize("@ss.hasPermi('cms:jiayi_news:edit')")
    @Log(title = "新闻动态" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JiayiNewsRequest request) {
        return AjaxResult.toAjax(jiayiNewsService.updateJiayiNews(request));
    }

    /**
     * 删除新闻动态
     */
    @PreAuthorize("@ss.hasPermi('cms:jiayi_news:remove')")
    @Log(title = "新闻动态" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return AjaxResult.toAjax(jiayiNewsService.deleteJiayiNewsByIds(ids));
    }
}
